<?php namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model{
	protected $table="goods";
	protected $validate=array(
		array('gname','required','商品名称不能为空',3,3)
	);
	protected $auto=[
        ['admin_aid','getAid','method',3,1],
        ['gnumber','getNum','method',3,1],
        ['time','getTime','method',3,1]
    ];
	public function store(){
		if(!$this->create()) return false;
		$cid=Q('post.category_cid');
		
		$cateModel=new \Common\Model\Cate;
		$cate=$cateModel->where("cid={$cid}")->pluck('type_tid');

		$this->data['type_tid']=$cate;
		$gid=$this->add();
		//2、添加detail表
		$detailModel = new \Common\Model\Detail;
		$thumb=implode(',',Q('post.thumb'));
		$detailModel->create();
		$detailModel->data['thumb'] = $thumb;
		$detailModel->data['gid'] = $gid;
		$detailModel->add();
		//添加商品属性表
		$attrModel=new \Common\Model\GoodsAttr;
		$attr=Q('post.attr');
		
		foreach($attr as $taid=>$v) {
			if($v){
				$data=array(
					'gtvalue'=>$v,
					'type_attr_taid'=>$taid,
					'goods_gid'=>$gid
				);
				$attrModel->add($data);
			}

		}
		
		// 添加规格
		$spec=Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					$data=array(
					'gtvalue'=>$vv,
					'type_attr_taid'=>$taid,
					'goods_gid'=>$gid,
					'added'=>$spec['added'][$taid][$kk]
				);
				$attrModel->add($data);
				}
			}
		}
		
		return true;
	}



	
	public function getAid(){
		$admin=$_SESSION['info']['aid'];
		return $admin;
	}
	public function getNum(){
		return time();
	}
	public function getTime(){
		return time();
	}



	//获得除了自己和自己的子集的分类数据
	public function getNoMy($cid){
		//1.获得自己的子集的cid
		$cids = $this->getSon($this->get(),$cid);
		//压入自己的cid
		$cids[] = $cid;
		//2.排除自己和子集的cid的数据
		//SELECT * FROM category WHERE cid NOT IN(2,3,5);
		$where = "cid NOT IN(" . implode(',', $cids) . ")";
		return $this->where($where)->get();
	}
	
	//获得子集
	public function getSon($data,$cid){
		$temp = array();
		//循环所有数据
		foreach ($data as $v) {
			//如果找到当前分类的子集
			if($v['pid'] == $cid){
				//压入到临时数组
				$temp[] = $v['cid'];
				//递归调用不断合并
				$temp = array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		//得到最终结果，返出
		return $temp;
	}

	public function edit(){
		p(Q('post.'));
		if(!$this->create()) return false;
		$gid=Q('post.gid',0,'intval');
		$pic=$this->where("gid={$gid}")->pluck('pic');
		p($pic);
		// unlink($pic);
		// Upload/Content/16/08/3351471568469.jpg
		$this->save();
		$detail=new \Common\Model\Detail;
		// 接受隐藏的gid给商品详细表判断
		
		$detail->create();
		$detail->where("detail.gid={$gid}")->save();

		//商品属性表先删除再增加
		$attrModel=new \Common\Model\GoodsAttr;
		$attrModel->where("goods_gid={$gid}")->delete();

		$attr=Q('post.attr');
		// p($attr);
		foreach($attr as $taid=>$v) {
			if($v){
				$data=array(
					'gtvalue'=>$v,
					'type_attr_taid'=>$taid,
					'goods_gid'=>$gid
				);
				$attrModel->add($data);
			}

		}

		// 添加规格
		$spec=Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					$data=array(
					'gtvalue'=>$vv,
					'type_attr_taid'=>$taid,
					'goods_gid'=>$gid,
					'added'=>$spec['added'][$taid][$kk]
				);
				$attrModel->add($data);
				}
			}
		}
		$goodList=new \Common\Model\GoodsList;
		$goodList->where("goods_gid=$gid")->delete();
		return true;
	}

}